﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Indigo.Quibbler.Data;
using System.Configuration;
using System.Windows.Threading;
using System.Windows.Media.Effects;

namespace Indigo.Quibbler.Controls
{
    /// <summary>
    /// Interaction logic for SectionControl.xaml
    /// </summary>
    public partial class SectionControlOverview : UserControl
    {
        private DispatcherTimer _effectTimer = new DispatcherTimer();

        public SectionControlOverview()
        {
            this.InitializeComponent();
            _effectTimer.Interval = TimeSpan.FromSeconds(10);
            _effectTimer.Tick += new EventHandler(_EffectTimer_Tick);
        }
        #region Dependency Property
        public string Category
        {
            get { return (string)GetValue(CategoryProperty); }
            set { SetValue(CategoryProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CategoryProperty =
            DependencyProperty.Register("Category", typeof(string), typeof(SectionControlOverview), new PropertyMetadata(OnCategoryChanged));


        public bool IsUpdated
        {
            get { return (bool)GetValue(IsUpdatedProperty); }
            set { SetValue(IsUpdatedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsUpdated.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsUpdatedProperty =
            DependencyProperty.Register("IsUpdated", typeof(bool), typeof(SectionControlOverview), new PropertyMetadata(IsUpdatedChanged));

        #endregion

        #region Private Static Methods
        private static void OnCategoryChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            (obj as SectionControlOverview).OnSectionControlCategoryChanged(e);
        }
        private static void IsUpdatedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            (obj as SectionControlOverview).OnIsUpdatedChanged(e);
        }
        #endregion

        void _EffectTimer_Tick(object sender, EventArgs e)
        {
            gridUpdated.Visibility = Visibility.Collapsed;
            _effectTimer.Stop();
            this.SetValue(SectionControlOverview.IsUpdatedProperty, false);
        }

        private void OnSectionControlCategoryChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue.ToString().ToLower().Equals("photos"))
            {
                gridImage.Visibility = Visibility.Visible;
                //gridUpdated.Margin = new Thickness(0, 15, 0, 15);
                BitmapDecoder decoder = BitmapDecoder.Create(new Uri("pack://application:,,/Quibbler - Universal News Reader;component/Images/SectionPhotos.png", UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                ImageSource iconSource = decoder.Frames[0];
                img.Source = iconSource;
            }
            else if (e.NewValue.ToString().ToLower().Equals("videos"))
            {
                gridImage.Visibility = Visibility.Visible;
                BitmapDecoder decoder = BitmapDecoder.Create(new Uri("pack://application:,,/Quibbler - Universal News Reader;component/Images/SectionVideos.png", UriKind.RelativeOrAbsolute), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                ImageSource iconSource = decoder.Frames[0];
                img.Source = iconSource;
            }
            else
                gridCategory.Visibility = Visibility.Visible;
            //this.SetValue(SectionControlOverview.IsUpdatedProperty, true);
        }

        private void OnIsUpdatedChanged(DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                gridUpdated.Visibility = Visibility.Visible;
                _effectTimer.Start();
            }
        }
    }
}